<?php
Yii::import('ext.widgets.EAbuse.AbuseData');
class AbuseButtonWidget extends Widget {
    
    public $model;
    public $route;
    
    public $buttonCssClass='abuse';
    
    public $onButtonLabel='';
    public $onButtonCssClass='abuse-on';
    
    public $offButtonLabel='Report';
    public $offButtonCssClass='abuse-off';
    
    public $id;
    public $data;
    
    /**
     * Initializes the widgets
     */
    public function init() {
        parent::init();
        if(empty($this->model)){
            echo __CLASS__.': "model" is not assigned.'; return;
        }
        
        if($this->route===null) 
            $this->route=$this->controller->id.'/abuse'.get_class($this->model);
        
        if($this->id===null)
            $this->id=implode('_',array(
                __CLASS__,
                get_class($this->model),
                '',
            ));
        
        if(Yii::app()->user->isGuest){
            $this->data=array();
        }else{
            $this->data=AbuseData::getData(get_class($this->model));
            $this->script();
        }
        
        
    }

    /**
     * Execute the widgets
     */
    public function run() {
        Yii::beginProfile(get_class($this));
        
        echo $this->link();
        
        Yii::endProfile(get_class($this));
    }
    
    private function script(){
        Yii::app()->clientScript->registerScript(__CLASS__.get_class($this->model), '
            $(document).ready(function() {
                $("[id^=\''.$this->id.'\']").click(function(){
                    var id = $(this).attr(\'id\');
                    id=id.replace("'.$this->id.'", "");
                    $.ajax({
                        url:"'.$this->controller->createUrl($this->route).'",
                        type:"POST",
                        data: "'.implode('&',array(
                            'ajax=ajax',
                            'YII_CSRF_TOKEN='.Yii::app()->request->csrfToken,
                            'id='
                        )).'"+(id),
                        dataType:"json",
                        success: function(data, textStatus, jqXHR){
                            if(data.result==1){
                                $("#'.$this->id.'"+(id)).html("'.$this->onButtonLabel.'");
                                $("#'.$this->id.'"+(id)).removeClass("'.$this->offButtonCssClass.'").addClass("'.$this->onButtonCssClass.'");
                            }else{
                                $("#'.$this->id.'"+(id)).html("'.$this->offButtonLabel.'");
                                $("#'.$this->id.'"+(id)).removeClass("'.$this->onButtonCssClass.'").addClass("'.$this->offButtonCssClass.'");
                            }
                        },
                        error: function(jqXHR, textStatus, errorThrown){
                            alert(textStatus);
                        }
                    });

                    return false;
                });
            });
            ',CClientScript::POS_END);
    }
    
    public function link(){
        return CHtml::link(
                in_array($this->model->id, $this->data)?
                    $this->onButtonLabel:$this->offButtonLabel, 
                $this->controller->createUrl($this->route, array(
                    'id'=>$this->model->id,
                )),
                array(
                    'id'=>$this->id.$this->model->id,
                    'class'=>implode(' ', array(
                        $this->buttonCssClass,
                        $this->offButtonCssClass,
                    ))
                ));
    }
    
}